package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.website.util.CookieUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
public class IndexController {

    @Reference
    private IUserInfoRedisService userInfoRedisService;

    @RequestMapping("")
    public String index(HttpServletResponse resp, HttpServletRequest req){
        // 获取登陆信息
        // 获取cookie信息
//        String token = null;
//        Cookie[] cookies = req.getCookies();
//        if(cookies != null && cookies.length > 0){
//            for (Cookie cookie : cookies) {
//                if("token".equals(cookie.getName())){
//                    token = cookie.getValue();
//                    // 延长cookie的时间
//                    cookie.setMaxAge(RedisKeys.USER_LOGIN_TOKEN.getTime().intValue());
//                    resp.addCookie(cookie);
//                    break;
//                }
//            }
//        }
        String token = CookieUtil.getToken(req, resp);
        // 通过token 获取用户对象
        UserInfo user = userInfoRedisService.getUserByToken(token);
        // 共享到页面显示
        req.getSession().setAttribute("userInfo", user);
        return "index/index";
    }
}
